﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinesys.Entities;
using Chinesys.DataModel;
using Chinesys.Domain.DataMapper;

namespace Chinesys.Domain
{
    public static class CategoryDomain
    {
        public static CategoriesEntity GetCategoryById(int id)
        {
            CategoriesEntity category;
            using (ChinesysEntities db = new ChinesysEntities())
            {
                category = CategoriesDataMapper.CategoriesFromRowToEntity(db.Categories.SingleOrDefault(x => x.Id == id));
            }

            return category;
        }

        public static List<CategoriesEntity> GetAll()
        {
            List<CategoriesEntity> categories;
            using (ChinesysEntities db = new ChinesysEntities())
            {
                List<Category> result = (from o in db.Categories
                              select o).ToList();

                if (result.Any())
                {
                    categories = new List<CategoriesEntity>();
                    result.ForEach(x => categories.Add(CategoriesDataMapper.CategoriesFromRowToEntity(x)));
                }
                else
                    categories = null;
            }

            return categories;
        }

    }
}
